package edu.nuc.projdemo.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.nuc.projdemo.common.Tools;

public class SessionFilter implements Filter {

	

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain arg2) throws IOException, ServletException {
		
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;
		
		
		/*if(request.getServletPath().endsWith(".css")||request.getServletPath().endsWith(".js")||request.getServletPath().endsWith(".gif")
				||request.getServletPath().endsWith(".png")){
			
			arg2.doFilter(req, res);
			return;
		
		}*/
		if(request.getServletPath().endsWith(".jsp")||request.getServletPath().equals("/sys/user")){
			
			arg2.doFilter(req, res);
			
			
		}else{
			if(request.getSession().getAttribute("user")!=null){
				
				
				arg2.doFilter(req, res);
			}else{
				
				response.sendRedirect(Tools.Basepath(request, response) +"sys/login.jsp");
			}
		}
	}

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}



}
